﻿/********************************************************************************/
/* 	0724th - shot.d																*/
/*------------------------------------------------------------------------------*/
/*	製作		（　゜ワ゜）ノ　／　松久貫太									*/
/*	製作開始　	2007/05/11														*/
/*	MAIL		omega@personal.email.ne.jp										*/
/*	URL		http://nagoya.cool.ne.jp/o_mega										*/
/*																				*/
/*	このソースは「やらわかライセンス」の元で配布されています。					*/
/*-更新履歴---------------------------------------------------------------------*/
/*	2007/--/--																	*/
/*-その他-----------------------------------------------------------------------*/
/*	なし																		*/
/********************************************************************************/

private import hell2;
private import actor;
private import vector;
private import screen;
private import player;
private import effect;
private import state;
private import std.math;


// 弾の種類
enum SHOTTYPE
{
	NORMAL,
	BREAK,
	ACCEL,
};
float SHOTSIZE[] = [0.2 , 0.2 , 0.2];
// 弾の色
enum SHOTCOLOR
{
	BLUE,
	RED,
	GREEN,
	WHITE,
};

class ShotActor : ActorPool!(Shot)
{
public:
	GameMain gamemain;
	
	this(GameMain gm,int n)
	{
		gamemain = gm;
		super(n);
	}
	
	void fire(Vec3 p ,Vec3 v,SHOTTYPE ty ,SHOTCOLOR cl)
	{
		Shot s = this.getInstance();
		if(s)
		{
			s.init(gamemain,p,v,ty,cl);
		}
	}
	
	Shot getNearestShot(Vec3 v)
	{
		float dist = 99999999.0;
		Shot result = null;
		
		foreach(Shot s;actors)
		{
			if(s.exist)
			{
				float d = s.position.getDistSqrt(v);
				if(d < dist)
				{
					dist = d;
					result = s;
				}
			}
		}
		return result;
	}
}

/* */
class Shot : Actor
{
private:
	static const int GRAZE_WAIT = 3;
public:
	Vec3 speed;
	Vec3 rotation;
	Vec3 rot_speed;
	SHOTTYPE type;
	SHOTCOLOR color;
	int timer;
	int graze_timer;
	GameMain gamemain;
	
	this()
	{
		super();
	}
	
	void init(GameMain gm,Vec3 p,Vec3 v,SHOTTYPE ty,SHOTCOLOR cl)
	{
		gamemain = gm;
		exist = true;
		position = new Vec3(p);
		speed = new Vec3(v);
		rotation = new Vec3();
		rot_speed = new Vec3( Hell_randInt(-100,100) / 4.0 ,
						Hell_randInt(-100,100) / 4.0 ,
						Hell_randInt(-100,100) / 4.0 );
		type = ty;
		color = cl;
		timer = 0;
		
		position.z = -20.0f;
	}
	
	void move()
	{
		position += speed;
		rotation += rot_speed;
		timer++;
		
		if(position.x > 30.0 || position.x < -30.0 || position.y > 30.0 || position.y < -30.0)
		{
			exist = false;
		}
		
		// graze
		Vec3 dist = new Vec3(gamemain.player.position - position);
		if(dist.getDist() < gamemain.player.getGrazeSize() * 1.414 + getSize() && graze_timer < 1)
		{
			gamemain.effectactor.add(position , new Vec3() , EFFECTTYPE.TRIANGLE);
			graze_timer = GRAZE_WAIT;
			gamemain.score++;
		}
		if(graze_timer > 0) graze_timer--;
	}
	
	void draw()
	{
		glPushMatrix();
		
		glLoadIdentity();
		glTranslatef(position.x , position.y ,position.z);
		glRotatef(rotation.x , rotation.y , rotation.z);
		
		float sz = getSize();
		glScalef(sz , sz , sz);
		glColor4f(0.8 , 0.8 , 0.8 , 0.3);
		BulletShape.drawPolygon();
		glColor4f(1.0 , 1.0 , 1.0 , 0.8);
		BulletShape.drawWire();
		
		glPopMatrix();
		/*
		resetPerspective();
		Vec3 dist = new Vec3(gamemain.player.position - position);
		Hell_drawFont(std.string.toString(cast(int)(dist.getDist() * 100) / 100.0),
			cast(int)(320 + position.x * 15) ,
			cast(int)(240 + position.y * 10) ,0.5 );
		setPerspective();
		*/
	}
	
	float getSize()
	{
		return SHOTSIZE[type];
	}
}